package com.itHsuDS.stack;

public class Calculator {
    public static void main(String[] args) {
        String expression="70+2*6-4";

        ArrayStack2 numStack=new ArrayStack2(10);
        ArrayStack2 operStack=new ArrayStack2(10);

        int index=0;//用于扫描
        int num1=0;
        int num2=0;
        int oper=0;
        int res=0;
        char ch=' ';
        String keepNum="";//用于拼接多位数

        while(true){
            ch=expression.substring(index,index+1).charAt(0);
            //判断是数字还是符号
            if(operStack.isOper(ch)){//符号
                //判断当前符号栈是否为空
                if(!operStack.isEmpty()){
                    if(operStack.priority(ch)<= operStack.priority(operStack.peek())){//符号优先级小于栈里的
                        num1=numStack.pop();
                        num2=numStack.pop();
                        oper=operStack.pop();
                        res=numStack.cal(num1,num2,oper);
                        //把运算的结果入栈
                        numStack.push(res);
                        operStack.push(ch);
                    }else {//符号优先级大于栈里的
                        operStack.push(ch);
                    }
                }else {
                    operStack.push(ch);
                }
            }else {//数字
                //numStack.push(ch-48);//因为这里的数字是字符，是ASCII码，要-48

                //分析思路：
                //1、当处理多位数时，不能发现是一个数就立即入栈，因为他可能是多位数
                //2、在处理数时，需要想expression的表达式的index后再看一位，如果还是数就进行扫描，如果是符号才入栈
                //3、需要定义一个字符串变量，进行拼接

                keepNum+=ch;

                //如果ch已经是expression的最后一位，就直接入栈
                if(index==expression.length()-1){
                    numStack.push(Integer.parseInt(keepNum));
                }else {
                    //判断下一个字符是不是数字，是则继续扫描，如果是运算符则入栈
                    //index本身不变，只是看后面的
                    if(operStack.isOper(expression.substring(index+1,index+2).charAt(0))){
                        //如果后一位是运算符，则入栈
                        //将字符转为int
                        numStack.push(Integer.parseInt(keepNum));
                        //keepNum清空
                        keepNum="";
                    }
                }
            }
            index++;
            if(index>=expression.length()){//扫描结束
                break;
            }
        }

        while (true){
            //如果符号栈为空，则计算出最后的结果
            if(operStack.isEmpty()){
                break;
            }
            num1=numStack.pop();
            num2=numStack.pop();
            oper=operStack.pop();
            res=numStack.cal(num1,num2,oper);
            numStack.push(res);
        }
        System.out.println(expression+"="+numStack.pop());
    }
}

class ArrayStack2{
    private int maxSize;
    private int[] stack;//数组模拟栈，存放数据
    private int top=-1;//表示栈顶，初始化为-1

    public ArrayStack2(int maxSize){
        this.maxSize=maxSize;
        stack=new int[maxSize];
    }

    public int peek(){
        return stack[top];
    }

    public boolean isFull(){
        return top==maxSize-1;//数组下标从0开始
    }

    public boolean isEmpty(){
        return top==-1;
    }

    public void push(int value){
        if(isFull()){
            System.out.println("栈满");
            return;
        }
        stack[++top]=value;
    }

    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("栈空");
        }
        return stack[top--];
    }

    //遍历时，从栈顶开始显示数据
    public void list(){
        if(isEmpty()){
            System.out.println("栈空");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.println("stack["+i+"]="+stack[i]+" ");
        }
    }

    //返回运算符的优先级，假定优先级用数字表示
    public int priority(int oper){
        if(oper=='*'||oper=='/'){
            return 1;
        }else if(oper=='+'||oper=='-'){
            return 0;
        }else {
            return -1;
        }
    }

    //判断是不是一个运算符
    public boolean isOper(char val){
        return val=='+'||val=='-'||val=='*'||val=='/';
    }

    public int cal(int num1,int num2,int oper){
        int res=0;
        switch (oper){
            case '+':
                res=num1+num2;
                break;
            case '-':
                res=num2-num1;
                break;
            case '*':
                res=num1*num2;
                break;
            case '/':
                res=num2/num1;
                break;
            default:
                break;
        }
        return res;
    }
}
